<?php
/**
 * Created by PhpStorm.
 * User: zk
 * Date: 2018/9/29
 * Time: 下午2:29
 */

namespace app\models;
use yii\db\ActiveRecord;

class Gl_feedback extends ActiveRecord {
    //获取意见列表
    public function getFeedbackLists($limit, $offset, $cityCode, $startTime, $endTime, $userName, $feedbackStatus) {
        $post = Gl_feedback::find()->JoinWith('user');

        if ($cityCode != -1){
            $post->andWhere(['user_user.city_code' => $cityCode]);
        }

        if ($startTime != -1) {
            $post->andWhere(['between', 'gl_feedback.gmt_create', $startTime, $endTime]);
        }

        if ($userName != -1){
            $post->andWhere(['like', 'user_user.user_nickname', $userName]);
        }

        if ($feedbackStatus != -1) {
            $post->andWhere(['gl_feedback.is_deleted' => $feedbackStatus]);
        }

        $count = $post->count();
        $result = $post->orderBy('gl_feedback.feedback_id DESC')->limit($limit)->offset($offset)->asArray()->all();

        return ['total'=>$count, 'rows'=>$result];
    }

    //更改意见状态
    public function updateFeedbackStatus($feedbackId, $status, $feedbackRmk) {
        $post = Gl_feedback::findOne(['gl_feedback.feedback_id'=>$feedbackId]);
        if ($feedbackRmk != -1) {
            $post->feedback_rmk = $feedbackRmk;
        }
        if ($status != -1) {
            $post->is_deleted = $status;
        }

        return $post->save();
    }

    //关联用户表 获取用户信息
    public function getUser()
    {
        //同样第一个参数指定关联的子表模型类名
        //第二个参数第一个是字表的 第二个是主表的
        return $this->hasOne(User_user::className(), ['user_id' => 'user_id']);
    }

    // 修改意见处理状态
    public function editStatus($feedbackId, $status) {
        $data = Gl_feedback::updateAll(['is_deleted' => $status], ['feedback_id' => $feedbackId]);

        return $data;
    }

//    查看备注
    public function showFeedbackRmk($feedbackId) {
        $result = Gl_feedback::find()->where(['feedback_id' => $feedbackId])->asArray()->one();

        return $result;
    }
}